package com.example.controller;

import com.example.entity.Adminuser;
import com.example.service.AdminuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;

@Controller
public class Loginctrl {
    @Autowired
//    @Resource
            AdminuserService adminuserService;

    @GetMapping("/tologin")
    public String toLogin() {
        return "Login";
    }

    @PostMapping("/dologin")
    public String doLogin(Adminuser adminuser, Model model, HttpSession httpSession) {
//        System.out.println("进行登录处理" + adminuser.getAduname() + "--" + adminuser.getAdupwd());
        adminuser.setAdupwd( DigestUtils.md5DigestAsHex(adminuser.getAdupwd().getBytes()));
        Adminuser admin = adminuserService.findAdmin(adminuser);
        if(admin == null){
            model.addAttribute("msg","用户名或者密码！");
            return "login";
        }else {
            httpSession.setAttribute("aduName",admin.getAduname());
            return "redirect:/admin/goods";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession httpSession, SessionStatus sessionStatus){
        sessionStatus.setComplete();
        httpSession.invalidate();
        return "login";
    }


}
